package leetcode_core.leetcode_1;

public class CountNodes {
      public class TreeNode {
      int val;
      CountNodes.TreeNode left;
      CountNodes.TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, CountNodes.TreeNode left, CountNodes.TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

    public int countNodes(TreeNode root) {
        TreeNode l = root;
        TreeNode r = root;
        int hl=0,hr=0;
        while(l!=null){
            l = l.left;
            hl++;
        }
        while(r!=null){
            r = r.right;
            hr++;
        }
        if(hr == hl){
            int h = hr;
            return (int)Math.pow(2,hr)-1;
        }
        else {
            return 1 + countNodes(root.left) + countNodes(root.right);
        }
    }



}
